<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('reviewRiskList.riskSetting.table.type')">
          <el-select v-model="searchForm.businessId" :placeholder="$t('reviewRiskList.riskSetting.prompt.type')">
            <el-option
              v-for="item in $t('reviewRiskList.riskSetting.type')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reviewRiskList.riskSetting.table.required')">
          <el-row>
            <el-col :span="13">
              <el-select v-model="searchForm.status" :placeholder="$t('reviewRiskList.riskSetting.prompt.required')">
                <el-option
                  v-for="item in $t('reviewRiskList.riskSetting.required')"
                  :key="item.value"
                  :label="item.desc"
                  :value="item.value"
                />
              </el-select>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item :label="$t('reviewRiskList.riskSetting.table.status')">
          <el-select v-model="searchForm.activate" :placeholder="$t('reviewRiskList.riskSetting.prompt.status')">
            <el-option
              v-for="item in $t('reviewRiskList.riskSetting.status')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        class="Top"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          :label="tableText['number']"
          min-width="50"
        />
        <el-table-column
          prop="functionName"
          :label="tableText['name']"
          min-width="150"
        />
        <el-table-column
          prop="remark"
          :label="tableText['remarks']"
          min-width="150"
        />
        <el-table-column
          prop="businessId"
          :label="tableText['type']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setState(scope.row['businessId'], 'riskSetting.type') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          :label="tableText['required']"
          min-width="100"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.id === 5">
              {{ setState(scope.row['status'], 'manageModule.requiredType') }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="exemptionDays"
          :label="$t('reviewRiskList.manageModule.uncheck')"
          min-width="100"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.id === 6">
              {{ scope.row['exemptionDays'] !== null ? scope.row['exemptionDays'] : null }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="exemptionDays"
          :label="$t('reviewRiskList.manageModule.face')"
          min-width="100"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.id === 7">
              {{ setState(scope.row['exemptionDays'], 'manageModule.faceType') }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="exemptionDays"
          :label="$t('reviewRiskList.manageModule.expirationDate')"
          min-width="150"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.id === 8">
              {{ scope.row['exemptionDays'] !== null ? scope.row['exemptionDays'] : null }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          prop="activate"
          :label="tableText['status']"
          min-width="100"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.activate < 2">
              {{ setState(scope.row['activate'], 'manageModule.statusType') }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editInfo(scope.row)">{{ $t('common.operate.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <risk-setting-dialog :show-dialog="editDialog" :choose-item="chooseItem" @change-dialog="changeDialog" @edit-success="editSuccess" />
  </el-row>
</template>

<script>
import { riskConfig } from '@/api/risk'
import riskSettingDialog from '@/components/risk/businessConfig/risk-setting-dialog'
export default {
  name: 'RiskSetting',
  components: {
    riskSettingDialog
  },
  data() {
    return {
      loading: true,
      editDialog: false,
      tableText: {},
      tableData: [],
      searchForm: this.params(),
      chooseItem: {
        id: null,
        businessId: null,
        exemptionDays: null,
        status: null,
        activate: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('reviewRiskList.riskSetting.table')
    this.getData()
  },
  methods: {
    params() {
      return {
        businessId: '',
        status: '',
        activate: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    getData() {
      this.loading = true
      riskConfig(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    setState(state, data) {
      const map = this.$t(`reviewRiskList.${data}`)
      const name = map.filter(item => {
        return item.value === state
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    editInfo(data) {
      this.choose(data)
      this.editDialog = true
    },
    changeDialog(param) {
      this.editDialog = param
      setTimeout(() => {
        this.choose({})
      }, 10)
    },
    editSuccess() {
      this.getData()
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.businessId = param.businessId
      this.chooseItem.exemptionDays = param.exemptionDays
      this.chooseItem.status = param.status
      this.chooseItem.activate = param.activate
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
